﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;
using DevExpress.Web.Mvc;
using System.Data;
using DevExpress.Web.ASPxUploadControl;
using System.IO;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 相册管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AlbumController : AdminBaseController
    {
        //
        // GET: /Admin/Album/


        /// <summary>
        /// 相册类别BLL
        /// </summary>
        private BLL.AlbumCls albumClsBLL = new BLL.AlbumCls();
        /// <summary>
        /// 相册类别权限BLL
        /// </summary>
        private BLL.AlbumAuth albumAuthBLL = new BLL.AlbumAuth();
        /// <summary>
        /// 通用方法BLL
        /// </summary>
        private BLL.Common commonBll = new BLL.Common();
        /// <summary>
        /// 相册BLL
        /// </summary>
        private BLL.Album albumBLL = new BLL.Album();
        /// <summary>
        /// 人员BLL
        /// </summary>
        private BLL.HREmpl empBll = new BLL.HREmpl();
        /// <summary>
        /// 相片明细BLL
        /// </summary>
        private BLL.AlbumEntry albumEntryBLL = new BLL.AlbumEntry();
        /// <summary>
        /// 相册管理首页
        /// </summary>
        /// <returns></returns>
        [AuthorityActionFilter("m_Album", Order = 1)]
        [LogPageActionFilter("相册管理", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 相册类别树
        /// </summary>
        /// <param name="focusNodeName"></param>
        /// <returns></returns>
        public ActionResult AlbumClsTree()
        {
            string where = "";
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string logid = OperateSession.get(GlobalConstant.PARAM_LOGINID);
            string focusNodeName = "";//默认为第一个类别
            if (logid != "admin")
            {
                where += " FAlbumClsNo IN (SELECT FAlbumClsNo FROM t_AlbumAuth WHERE FEntityNo='" + GlobalConstant.ENTITYNO + "' AND FEmplNo='" + emplNo + "')";
            }
            List<Model.AlbumCls> albumClsList = albumClsBLL.GetModelList(where);
            ElingOA6_Admin_DSJCJ.Models.TreeModelList treeModelList = new TreeModelList();
            if (albumClsList.Count > 0)
            {
                foreach (Model.AlbumCls albumClsModel in albumClsList)
                {
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = DevExpressHelper.GetUrl(new { Controller = "Album", Action = "AlbumList" }) + "|" + albumClsModel.FAlbumClsNo,
                        Name = albumClsModel.FAlbumClsName,
                        ImageUrl = Url.Content("~/Content/ControlImages/user_group.png")
                    };
                    treeModelList.Add(treeParentItemModel);
                }
            }
            ViewData["focusNodeName"] = focusNodeName;
            return View(treeModelList);
        }
        /// <summary>
        /// 相册列表
        /// </summary>
        /// <returns></returns>
        public ActionResult AlbumList(string albumClsNo, int pageIndex = 1)
        {
            string fiedls = "*";
            int total;
            int pageRowSize = 15;
            string where = " FAlbumClsNo='" + albumClsNo + "'";

            DataSet ds = commonBll.GetListByPage("t_Album", fiedls, pageRowSize, pageIndex, where, "FAlbumId", 1, out total);

            List<Model.Album> albumAllList = new List<Model.Album>();
            List<Model.Album> albumList = albumBLL.DataTableToList(ds.Tables[0]);

            for (int i = 0; i < total; i++)
            {
                albumAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (albumList.Count > i)
                {
                    albumAllList[((pageIndex - 1) * pageRowSize) + i] = albumList[i];
                }
            }
            ViewData["albumClsNo"] = albumClsNo;
            ViewData["pageRowSize"] = pageRowSize;
            return View("AlbumList", albumAllList);
        }
        /// <summary>
        /// 相册列表具体执行方法
        /// </summary>
        /// <returns></returns> 
        public ActionResult AlbumListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int PageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string albumClsNo = Request.Params["albumClsNo"] == null ? "" : Request.Params["albumClsNo"].ToString();
            string callbackArg = DevExpress.Web.Mvc.DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();
            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = PageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }

            string fiedls = "*";
            int total;
            string where = " FAlbumClsNo='" + albumClsNo + "'";

            DataSet ds = commonBll.GetListByPage("t_Album", fiedls, pageRowSize, pageIndex, where, "FAlbumId", 1, out total);

            List<Model.Album> albumAllList = new List<Model.Album>();
            List<Model.Album> albumList = albumBLL.DataTableToList(ds.Tables[0]);

            for (int i = 0; i < total; i++)
            {
                albumAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (albumList.Count > i)
                {
                    albumAllList[((pageIndex - 1) * pageRowSize) + i] = albumList[i];
                }
            }
            ViewData["pageRowSize"] = pageRowSize;
            return PartialView(albumAllList);
        }

        /// <summary>
        /// 新建相册
        /// </summary>
        /// <param name="dirID"></param>
        /// <param name="docID"></param>
        /// <returns></returns>
        public ActionResult AlbumAdd(string albumClsNo, int albumId = 0)
        {
            string loginId = OperateSession.get(GlobalConstant.PARAM_LOGINID);
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string imgPath = Url.Content("~/Content/images/Admin/zwImg.bmp");
            Model.Album albumModel = new Model.Album();

            if (albumId == 0)//新增
            {
                albumModel.FAlbumClsNo = albumClsNo;
                albumModel.FDispSeq = albumBLL.GetDirMaxSeq(albumClsNo, GlobalConstant.ENTITYNO) + 1;
                albumModel.FShowDate = DateTime.Now;
                albumModel.FIsSecret = "N";
                albumModel.FEmpName = empBll.GetEmpName(GlobalConstant.ENTITYNO, emplNo);
                albumModel.FUpdateTime = DateTime.Now;
            }
            else
            {
                albumModel = albumBLL.GetModel(albumId);

                if (null != albumModel.FContentBin && albumModel.FContentBin.Length > 0)
                {
                    imgPath = Url.Action("PhotoShow", "Album", new { albumId = albumId });
                }
                albumModel.FEmpName = empBll.GetEmpName(albumModel.FEntityNo, albumModel.FEmplNo);
                albumModel.FUpdateTime = null == albumModel.FUpdateTime ? albumModel.FCreateDate : albumModel.FUpdateTime;

            }

            ViewData["imgPath"] = imgPath;

            return View(albumModel);
        }

        //AlbumAddSave
        /// <summary>
        /// 相册增加修改保存
        /// </summary>
        /// <param name="docModel"></param>
        public void AlbumAddSave(Model.Album albumModel)
        {
            string bgMusicPath = string.Empty;
            UploadedFile[] fileList = UploadControlExtension.GetUploadedFiles("FBGMusicPathImg");
            string message = string.Empty;
            if (null != fileList && fileList.Length > 0)
            {
                UploadedFile upload = fileList[0];
                if (upload.ContentLength > 0)
                {
                    string fileSavePath = System.Configuration.ConfigurationManager.AppSettings["FileSavePath"] + "DirFiles/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    try
                    {
                        int maxSize = 30 * 1024 * 1024;
                        string musicUrl = FileUpload.UploadFile(upload, fileSavePath, ".mp3.mid.wma", maxSize);
                        if (musicUrl == "1")
                        {
                            message = "背景音乐文件为空";
                        }
                        else if (musicUrl == "2")
                        {
                            message = "背景音乐文件类型错误";
                        }
                        else if (musicUrl == "3")
                        {
                            message = "背景音乐文件超出大小，最大" + (maxSize / 1024 / 1024) + "MB";
                        }
                        else if (musicUrl == "4")
                        {
                            message = "背景音乐文件已存在";
                        }
                        else
                        {
                            bgMusicPath = fileSavePath + "/" + musicUrl;
                        }

                    }
                    catch (Exception)
                    {
                        message = "背景音乐上传失败";
                    }
                }
            }

            if (message != string.Empty)
            {
                Response.Write("<script>alert('" + message + "');window.location.href ='" + Url.Action("AlbumAdd", "Album", new { albumClsNo = albumModel.FAlbumClsNo }) + "';</script>");
                Response.Flush();
                Response.End();
                return;
            }

            byte[] img = null;

            fileList = UploadControlExtension.GetUploadedFiles("FContentBinImg");
            if (null != fileList && fileList.Length > 0)
            {
                UploadedFile upload = fileList[0];
                if (upload.ContentLength > 0)
                {
                    try
                    {
                        img = FileUpload.setImgByByte(upload.FileBytes, string.Empty, 165, 98, 2);
                    }
                    catch (Exception)
                    {
                        Response.Write("<script>alert('封面图片生成缩略图失败');window.location.href='" + Url.Action("AlbumAdd", "Album", new { albumClsNo = albumModel.FAlbumClsNo }) + "';</script>");
                        Response.Flush();
                        Response.End();
                        return;
                    }

                }
            }
            string picPath = "";
            //上传原始图片
            if (null != fileList && fileList.Length > 0)
            {
                UploadedFile upload = fileList[0];
                if (upload.ContentLength > 0)
                {
                    UploadPic(upload, Fetch.Post("hdFAlbumClsNo"), ref message, ref picPath);
                }
            }
            if (message != string.Empty)
            {
                Response.Write("<script>alert('" + message + "');window.location.href ='" + Url.Action("AlbumAdd", "Album", new { albumClsNo = albumModel.FAlbumClsNo }) + "';</script>");
                Response.Flush();
                Response.End();
                return;
            }
            albumModel.FFilePath = picPath;//保存原始图片路径


            albumModel.FPlayInterval = albumModel.FPlayInterval == 0 ? 6 : albumModel.FPlayInterval;
            albumModel.FDispSeq = null == albumModel.FDispSeq ? 999 : albumModel.FDispSeq;
            albumModel.FShowDate = null == albumModel.FShowDate ? DateTime.Now : albumModel.FShowDate;
            albumModel.FIsSecret = "N";

            string oldBgMusicPath = string.Empty;
            bool isSave = true;
            albumModel.FAlbumId = int.Parse(Fetch.Post("hdFAlbumId"));
            if (albumModel.FAlbumId == 0)//增加
            {
                albumModel.FAlbumClsNo = Fetch.Post("hdFAlbumClsNo");
                albumModel.FFileType = "ALBUM";
                albumModel.FCreateDate = DateTime.Now;
                albumModel.FEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                albumModel.FEntityNo = GlobalConstant.ENTITYNO;
                albumModel.FDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                albumModel.FBGMusicPath = bgMusicPath;
                if (null != img && img.Length > 0)
                {
                    albumModel.FContentBin = img;
                }
                try
                {
                    albumBLL.DocAdd(albumModel);
                }
                catch (Exception ee)
                {
                    isSave = false;
                }

            }
            else
            {
                Model.Album updDocObj = albumBLL.GetModel(albumModel.FAlbumId);
                int oldSeq = null != updDocObj.FDispSeq ? updDocObj.FDispSeq.Value : 1;
                oldBgMusicPath = updDocObj.FBGMusicPath;

                if (bgMusicPath != string.Empty)
                {
                    updDocObj.FBGMusicPath = bgMusicPath;
                }

                if (null != img && img.Length > 0)
                {
                    updDocObj.FContentBin = img;
                }
                updDocObj.FAlbumClsNo = Fetch.Post("hdFAlbumClsNo");
                updDocObj.FFileName = albumModel.FFileName;
                updDocObj.FContentTxt = albumModel.FContentTxt;
                updDocObj.FEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                updDocObj.FDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                updDocObj.FPlayInterval = albumModel.FPlayInterval;
                updDocObj.FDispSeq = albumModel.FDispSeq;
                updDocObj.FShowDate = albumModel.FShowDate;
                updDocObj.FIsSecret = albumModel.FIsSecret;
                updDocObj.FUpdateTime = DateTime.Now;
                updDocObj.FFileNo = albumModel.FFileNo;
                updDocObj.FFileNoDate = albumModel.FFileNoDate;

                if (picPath != "")//如果有上传图片，才更新上传图片
                {
                    //如果存在老图片,先删除老图片
                    try
                    {
                        GenerateUtil.DeleteFile(updDocObj.FFilePath);
                    }
                    catch (Exception ex)
                    {
                        message = ex.ToString();
                    }
                    if (message != string.Empty)
                    {
                        Response.Write("<script>alert('" + message + "');window.location.href ='" + Url.Action("AlbumAdd", "Album", new { albumClsNo = albumModel.FAlbumClsNo }) + "';</script>");
                        Response.Flush();
                        Response.End();
                        return;
                    }
                    updDocObj.FFilePath = albumModel.FFilePath;
                }

                try
                {
                    albumBLL.UpdateDoc(updDocObj, oldSeq);
                }
                catch (Exception)
                {
                    isSave = false;
                }

            }

            if (isSave)
            {
                if (bgMusicPath != string.Empty && oldBgMusicPath != string.Empty)
                {
                    GenerateUtil.DeleteFile(oldBgMusicPath);
                }

                string refreType = albumModel.FAlbumId == 0 ? "add" : "edit";

                Response.Write("<script>parent.albumModalModeOK('" + refreType + "');</script>");
                Response.Flush();
                Response.End();
            }
            else
            {

                if (bgMusicPath != string.Empty)
                {
                    GenerateUtil.DeleteFile(bgMusicPath);
                }
                if (picPath != string.Empty)
                {
                    GenerateUtil.DeleteFile(picPath);
                }

                Response.Write("<script>alert('保存失败');window.location.href= '" + Url.Action("AlbumAdd", "Album", new { albumClsNo = albumModel.FAlbumClsNo }) + "';</script>");
                Response.Flush();
                Response.End();
            }



        }
        /// <summary>
        /// 相册缩略图显示
        /// </summary>
        /// <param name="docId"></param>
        /// <returns></returns>
        public ActionResult PhotoShow(int albumId)
        {
            byte[] photo = albumBLL.GetContentBin(albumId);

            if (null == photo)
            {
                //string defaultImg = "0xFFD8FFE000104A46494600010101006000600000FFDB004300080606070605080707070909080A0C140D0C0B0B0C1912130F141D1A1F1E1D1A1C1C20242E2720222C231C1C2837292C30313434341F27393D38323C2E333432FFDB0043010909090C0B0C180D0D1832211C213232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232FFC0001108006200A503012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00F75BB924430AC6FB77BE09001FE74FF265FF009F97FF00BE57FC2A2BDFF5B6BFF5D2AE50041E4CBFF3F2FF00F7CAFF00851E4CBFF3F2FF00F7CAFF00854F450041E4CBFF003F2FFF007CAFF851E4CBFF003F2FFF007CAFF854F450041E4CBFF3F2FF00F7CAFF00851E4CBFF3F2FF00F7CAFF00854F450041E4CBFF003F2FFF007CAFF851E4CBFF003F2FFF007CAFF854F450041E4CBFF3F2FF00F7CAFF00851E4CBFF3F2FF00F7CAFF00854F450041E4CBFF003F2FFF007CAFF851E4CBFF003F2FFF007CAFF854F450041E4CBFF3F2FF00F7CAFF00851E4CBFF3F2FF00F7CAFF00854F450041E4CBFF003F2FFF007CAFF851E4CBFF003F2FFF007CAFF854F450041E4CBFF3F2FF00F7CAFF00853658E6489D85CBE4027EEAFF008559A8EE3FE3DE4FF74D0036D9DA4B68DD8E599724D14DB3FF008F287FDDA28023BDFF005B6BFF005D2AE553BDFF005B6BFF005D2AE5001451450014514500145145001451450014514500145145001451450014514500151DC7FC7BC9FEE9A92A3B8FF8F793FDD34011D9FF00C7943FEED1459FFC7943FEED14011DEFFADB5FFAE9572A9DEFFADB5FFAE9572800A28A2800A28AE7B58F115C69574237B48C47D43BC87F783D178E1A803A1A2B0A6F111892D3FD1416B942C019080B8FF80E7F4AB1A7EAA2EBCF77BAB62883F8559761F42CDC1FD2803568AC1BBD56EADAD659D6FB4B93629608B9C9FF00C7AA48352B89228A47BFD2D4300CCBCE403DBEF5006D51593ABEAAF64968D6ED0959E4D9BD816007A80BD6B38EBDAA25CC90C96C14AB0098B6625FDF1BBA50074F456335FEA16F7D650CE2DD92E49042A32B2E067B93525D6B02CF588ED660161688BEE0ACCD9CF4E2803568AC6875F89AFA78E50CB6EA07952089F2C7BF6AD3B6BA86EE33242C5941C64A95FE740135145140054771FF001EF27FBA6A4A8EE3FE3DE4FF0074D004767FF1E50FFBB45167FF001E50FF00BB4500477BFEB6D7FEBA55CAA77BFEB6D7FEBA55CA0028A28A002B9CF1169A4A46D6B6F231965FDFECDC72BF8671F85747450072090DDC7219116F4390016FDF1381FF0001AD0D0E392DB479CDEDB4CDBA66628D1966607BE3A9ADFA280313FB39751BA8A57B4482D23F9951A30AECDEA7D07B1AA7A76CB3B19A39F4A99E7123B28FB2920FA738AE9E8A00E77524B8B9B4D2E536B3C2564DD225BA92D18F6AAD70AFF68464B5BE7404664F29CCA7DCE703F2AEAE8A00E7195E5D474EF2A2BF9121766792E2320818F522ACB584DA8EAAB7A5A7B58963318C36D76E7AFB0FAD6D514018C6DAEED758B4114F772DB32B79BE636E51E9DB8AD9A28A0028A28A002A3B8FF8F793FDD35254771FF1EF27FBA68023B3FF008F287FDDA28B3FF8F287FDDA28023BDFF5B6BFF5D2AE553BDFF5B6BFF5D2AE5001451450014514500145145001451450014514500145145001451450014514500151DC7FC7BC9FEE9A92A3B8FF008F793FDD34011D9FFC7943FEED1459FF00C7943FEED14011DEFF00ADB5FF00AE9572A9DE9C496DFF005D2ADEF5FEF0FCE80168A4DEBFDE1F9D1BD7FBC3F3A005A2937AFF00787E746F5FEF0FCE80168A4DEBFDE1F9D1BD7FBC3F3A005A2937AFF787E746F5FEF0FCE80168A4DEBFDE1F9D1BD7FBC3F3A005A2937AFF00787E746F5FEF0FCE80168A4DEBFDE1F9D1BD7FBC3F3A005A2937AFF787E746F5FEF0FCE8016A3B8FF8F793FDD34FDEBFDE1F9D4770CBF6793E61F74F7A00659FFC7943FEED1459FF00C7943FEED14012BAABAE19430F423351F9317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145001E4C5FF003C93FEF91479317FCF24FF00BE45145004C0600038028A28A00FFFD9";
                photo = new byte[0];
            }

            return new ImageResult()
            {
                byteStream = photo
            };

        }

        //DocDel
        /// <summary>
        /// 文件删除
        /// </summary>
        /// <param name="docID"></param>
        /// <param name="dirID"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public string DocDel(int albumId, string albumClsNo, string type = "file")
        {

            if (type == "ALBUM")//相册
            {

                int photoCount = albumEntryBLL.GetCountByFAlbumId(albumId);
                if (photoCount > 0)
                {
                    return "相册包含图片，请先删除图片再删除相册";
                }
            }

            Model.Album albumModel = albumBLL.GetModel(albumId);
            bool isDelOK = false;
            try
            {
                albumBLL.Delete(albumId);
                isDelOK = true;
                if (albumModel.FFilePath != string.Empty)
                {
                    GenerateUtil.DeleteFileJD(albumModel.FFilePath);
                }

                if (albumModel.FBGMusicPath != string.Empty)
                {
                    GenerateUtil.DeleteFile(albumModel.FBGMusicPath);
                }

                return "ok";
            }
            catch (Exception)
            {
                if (!isDelOK)
                {
                    return "删除失败";
                }
            }


            return string.Empty;
        }
        /// <summary>
        /// 相册图片列表
        /// </summary>
        /// <param name="docID"></param>
        /// <param name="dirID"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public ActionResult PicList(int albumId, string albumClsNo, int pageIndex = 1)
        {
            string fiedls = "*";
            int total;
            string where = " FAlbumId=" + albumId;
            int pageRowSize = 15;
            DataSet ds = commonBll.GetListByPage("t_AlbumEntry", fiedls, pageRowSize, pageIndex, where, "FAlbumEntryID", 0, out total);

            List<Model.AlbumEntry> albumEntryAllList = new List<Model.AlbumEntry>();
            List<Model.AlbumEntry> albumEntryList = albumEntryBLL.DataTableToList(ds.Tables[0]);

            for (int i = 0; i < total; i++)
            {
                albumEntryAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (albumEntryList.Count > i)
                {
                    albumEntryAllList[((pageIndex - 1) * pageRowSize) + i] = albumEntryList[i];
                }
            }
            ViewData["albumId"] = albumId;
            ViewData["albumClsNo"] = albumClsNo;
            ViewData["pageRowSize"] = pageRowSize;
            return View("PicList", albumEntryAllList);
        }
        /// <summary>
        /// 相册图片列表方法
        /// </summary>
        /// <returns></returns>
        public ActionResult PicListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int PageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            int albumId = null == Request.Params["albumId"] ? -1 : int.Parse(Request.Params["albumId"]);
            string callbackArg = DevExpress.Web.Mvc.DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();
            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = PageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }


            string fiedls = "*";
            int total;
            string where = " FAlbumId=" + albumId;

            DataSet ds = commonBll.GetListByPage("t_AlbumEntry", fiedls, pageRowSize, pageIndex, where, "FAlbumEntryID", 0, out total);

            List<Model.AlbumEntry> albumEntryAllList = new List<Model.AlbumEntry>();
            List<Model.AlbumEntry> albumEntryList = albumEntryBLL.DataTableToList(ds.Tables[0]);

            for (int i = 0; i < total; i++)
            {
                albumEntryAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (albumEntryList.Count > i)
                {
                    albumEntryAllList[((pageIndex - 1) * pageRowSize) + i] = albumEntryList[i];
                }
            }
            ViewData["pageRowSize"] = pageRowSize;
            return PartialView(albumEntryAllList);
        }

        /// <summary>
        /// 视频、图片批量上传
        /// </summary>
        /// <param name="dirID"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public ActionResult MediaUpload(string albumClsNo, string type)
        {
            bool IsShowFengM = false;
            string memo = string.Empty;

            string upload_url = string.Empty;
            string file_size_limit = string.Empty;
            string file_types = "*.*";
            string DesTitle = string.Empty;
            string file_upload_limit = string.Empty;
            string button_image_url = Url.Content("~/Content/images/XPButtonUploadText_61x22.png");

            switch (type)
            {
                case "pic":
                    {
                        int albumId = int.Parse(Request.QueryString["albumId"]);

                        upload_url = Url.Action("photoUploadSave", "Album", new { albumId = albumId });
                        //file_upload_limit = "1";
                        file_size_limit = "20480";
                        file_types = "*.jpg;*.jpeg;*.gif";
                        DesTitle = "批量上传相片 （支持的相片类型jpg,jpeg,gif,单个文件最大20M)";
                        break;
                    }
                case "video":
                    {
                        upload_url = Url.Action("VideoUploadSave", "Album", new { albumClsNo = albumClsNo });
                        file_upload_limit = "1";
                        file_size_limit = "2147483648";
                        file_types = "*.avi;*.wmv;*.mpg;*.flv";
                        DesTitle = "上传视频文件（支持的视频类型avi,wmv,mpg,flv)";
                        IsShowFengM = true;

                        memo = "<font style='color:red'>如果视频不是FLV格式，会自动转换为FLV格式，转换过程可能比较慢请耐心等待</font>";
                        break;
                    }
                case "updvideo":
                    {
                        int albumId = int.Parse(Request.QueryString["albumId"]);

                        upload_url = Url.Action("VideoUploadEditSave", "Album", new { albumClsNo = albumClsNo, albumId = albumId });
                        file_upload_limit = "1";
                        file_size_limit = "2147483648";
                        file_types = "*.avi;*.wmv;*.mpg;*.flv";
                        DesTitle = "上传视频文件（支持的视频类型avi,wmv,mpg,flv)";
                        IsShowFengM = true;

                        memo = "<font style='color:red'>如果视频不是FLV格式，会自动转换为FLV格式，转换过程可能比较慢请耐心等待1</font>";
                        break;
                    }
            }

            ViewData["button_image_url"] = button_image_url;
            ViewData["file_upload_limit"] = file_upload_limit;
            ViewData["DesTitle"] = DesTitle;
            ViewData["file_types"] = file_types;
            ViewData["file_size_limit"] = file_size_limit;
            ViewData["upload_url"] = upload_url;
            ViewData["albumClsNo"] = albumClsNo;
            ViewData["IsShowFengM"] = IsShowFengM;
            ViewData["memo"] = memo;
            return View();
        }
        /// <summary>
        /// 照片上传保存
        /// </summary>
        /// <param name="docID"></param>
        public void photoUploadSave(int albumId)
        {

            System.Web.HttpFileCollectionBase uploadFiles = Request.Files;
            HttpPostedFileBase theFile;

            string deptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
            string fileSavePath = System.Configuration.ConfigurationManager.AppSettings["FileSavePath"] + "DirFiles/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + deptNo;
            GenerateUtil.Addfolder(fileSavePath);

            for (int i = 0; i < uploadFiles.Count; i++)
            {
                theFile = uploadFiles[i];
                if (null != theFile && uploadFiles.GetKey(i).ToUpper() == "FILEDATA")//swfupload控件中的文档
                {


                    Model.AlbumEntry entryObj = new Model.AlbumEntry();
                    entryObj.FAlbumId = albumId;
                    entryObj.FDispSeq = albumEntryBLL.GetMaxDispSeq(albumId) + 1;
                    entryObj.FFileType = theFile.ContentType;

                    string _backname = theFile.FileName.Substring(theFile.FileName.LastIndexOf('.')); //文件后缀名

                    entryObj.FAlbumPhotoURL = fileSavePath + "/" + GenerateUtil.GenerateStringId() + _backname;
                    entryObj.FAlbumThumbURL = fileSavePath + "/" + GenerateUtil.GenerateStringId() + _backname;

                    string savePath = fileSavePath + "/" + GenerateUtil.GenerateStringId() + _backname;

                    int step = 0;

                    try
                    {
                        theFile.SaveAs(Server.MapPath(savePath));
                        step = 1;//用于生成缩略图的图片保存成功
                        FileUpload.setImg1(savePath, entryObj.FAlbumPhotoURL, 800, 490, 1);
                        step = 2;//大图片缩略图生成成功
                        FileUpload.setImg1(savePath, entryObj.FAlbumThumbURL, 100, 60, 2);
                        step = 3;//小图片缩略图生成成功

                        albumEntryBLL.Add(entryObj);

                        GenerateUtil.DeleteFile(savePath);

                        Response.Write("successed|ok");
                        Response.Flush();
                        Response.End();
                    }
                    catch (Exception)
                    {
                        if (step == 1)
                        {
                            GenerateUtil.DeleteFile(savePath);
                        }
                        else if (step == 2)
                        {
                            GenerateUtil.DeleteFile(savePath);
                            GenerateUtil.DeleteFile(entryObj.FAlbumPhotoURL);
                        }
                        else if (step == 3)
                        {
                            GenerateUtil.DeleteFile(savePath);
                            GenerateUtil.DeleteFile(entryObj.FAlbumPhotoURL);
                            GenerateUtil.DeleteFile(entryObj.FAlbumThumbURL);
                        }

                        Response.Write("successed|error");
                        Response.Flush();
                        Response.End();
                    }

                }
            }
        }

        /// <summary>
        /// 照片删除
        /// </summary>
        /// <param name="entryID"></param>
        /// <returns></returns>
        [HttpPost]
        public string EntryDel(int entryID)
        {
            Model.AlbumEntry entryObj = albumEntryBLL.GetModel(entryID);
            int step = 0;
            try
            {
                albumEntryBLL.Delete(entryID);
                step = 1;

                if (entryObj.FAlbumPhotoURL != string.Empty)
                {
                    GenerateUtil.DeleteFile(entryObj.FAlbumPhotoURL);
                }

                if (entryObj.FAlbumThumbURL != string.Empty)
                {
                    GenerateUtil.DeleteFile(entryObj.FAlbumThumbURL);
                }

                return "ok";
            }
            catch (Exception)
            {
                if (step == 0)
                {
                    return "删除失败";
                }
                else
                {
                    return "ok";
                }

            }

        }


        /// <summary>
        /// 照片批量删除
        /// </summary>
        /// <param name="entryIDs"></param>
        /// <returns></returns>
        [HttpPost]
        public string EntryDelMany(string entryIDs)
        {
            string[] entryArray = entryIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            List<int> entryIdList = new List<int>();
            try
            {
                entryArray.ToList().ForEach(c => entryIdList.Add(int.Parse(c)));
            }
            catch (Exception)
            {
                return "编号存在非法字符";
            }

            if (entryIdList.Count > 0)
            {
                entryIDs = string.Empty;
                entryIdList.ForEach(c => entryIDs += c + ",");
                if (entryIDs != string.Empty)
                {
                    entryIDs = entryIDs.TrimEnd(',');
                }
                else
                {
                    return "无删除编号";
                }
                try
                {
                    albumEntryBLL.DelMany(entryIDs);
                    return "ok";
                }
                catch (Exception)
                {
                    return "删除失败";
                }

            }
            else
            {
                return "无删除编号";
            }

        }
        /// <summary>
        /// 照片修改
        /// </summary>
        /// <param name="entryID"></param>
        /// <returns></returns>
        public ActionResult PicEdit(int entryID)
        {
            Model.AlbumEntry entryObj = albumEntryBLL.GetModel(entryID);
            return View(entryObj);
        }
        /// <summary>
        /// 照片修改保存
        /// </summary>
        /// <param name="entryModel"></param>
        public void PicEditSave(Model.AlbumEntry entryModel)
        {
            Model.AlbumEntry entryOldObj = albumEntryBLL.GetModel(entryModel.FAlbumEntryID);

            entryOldObj.FAlbumNote = entryModel.FAlbumNote;
            entryOldObj.FDispSeq = entryModel.FDispSeq;

            string newPhotoUrl = string.Empty;
            string newThumbUrl = string.Empty;
            UploadedFile[] fileList = UploadControlExtension.GetUploadedFiles("FAlbumPhoto");
            string message = string.Empty;
            if (null != fileList && fileList.Length > 0)
            {
                UploadedFile upload = fileList[0];
                if (upload.ContentLength > 0)
                {
                    string fileSavePath = System.Configuration.ConfigurationManager.AppSettings["FileSavePath"] + "DirFiles/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    string url = string.Empty;
                    int step = 0;
                    try
                    {
                        int maxSize = 20 * 1024 * 1024;
                        string musicUrl = FileUpload.UploadFile(upload, fileSavePath, ".jpg.jpeg.gif", maxSize);
                        if (musicUrl == "1")
                        {
                            message = "文件为空";
                        }
                        else if (musicUrl == "2")
                        {
                            message = "文件类型错误";
                        }
                        else if (musicUrl == "3")
                        {
                            message = "文件超出大小，最大" + (maxSize / 1024 / 1024) + "MB";
                        }
                        else if (musicUrl == "4")
                        {
                            message = "文件已存在";
                        }
                        else
                        {
                            step = 1;
                            url = fileSavePath + "/" + musicUrl;
                            //生成缩略图
                            string bakName = musicUrl.Substring(musicUrl.LastIndexOf('.'));
                            newPhotoUrl = fileSavePath + "/" + GenerateUtil.GenerateStringId() + bakName;
                            FileUpload.setImg1(url, newPhotoUrl, 800, 490, 1);
                            step = 2;//大图片缩略图生成成功
                            newThumbUrl = fileSavePath + "/" + GenerateUtil.GenerateStringId() + bakName;
                            FileUpload.setImg1(url, newThumbUrl, 100, 60, 2);
                            step = 3;//小图片缩略图生成成功

                            GenerateUtil.DeleteFile(url);

                            entryOldObj.FFileType = upload.ContentType;

                        }

                    }
                    catch (Exception)
                    {
                        message = "文件上传失败";
                        if (step == 1)
                        {
                            GenerateUtil.DeleteFile(url);
                        }
                        else if (step == 2)
                        {
                            GenerateUtil.DeleteFile(url);
                            GenerateUtil.DeleteFile(newPhotoUrl);
                        }
                        else if (step == 3)
                        {
                            GenerateUtil.DeleteFile(url);
                            GenerateUtil.DeleteFile(newPhotoUrl);
                            GenerateUtil.DeleteFile(newThumbUrl);
                        }

                    }
                }
            }

            if (message != string.Empty)
            {
                Response.Write("<script>alert('" + message + "');window.location.href='" + Url.Action("PicEdit", "DocManager", new { entryID = entryModel.FAlbumEntryID }) + "';</script>");
                Response.Flush();
                Response.End();
                return;
            }

            if (newPhotoUrl != string.Empty)
            {
                entryOldObj.FAlbumPhotoURL = newPhotoUrl;
            }
            if (newThumbUrl != string.Empty)
            {
                entryOldObj.FAlbumThumbURL = newThumbUrl;
            }


            try
            {
                albumEntryBLL.Update(entryOldObj);
                Response.Write("<script>parent.picEditModalModeOK();</script>");
                Response.Flush();
                Response.End();
            }
            catch (Exception)
            {
                if (newPhotoUrl != string.Empty)
                {
                    GenerateUtil.DeleteFile(newPhotoUrl);
                }
                if (newThumbUrl != string.Empty)
                {
                    GenerateUtil.DeleteFile(newThumbUrl);
                }
                Response.Write("<script>alert('保存失败');window.location.href= '" + Url.Action("PicEdit", "DocManager", new { entryID = entryModel.FAlbumEntryID }) + "';</script>");
                Response.Flush();
                Response.End();
            }


        }
        /// <summary>
        /// 图片播放页面
        /// </summary>
        /// <param name="docID"></param>
        /// <param name="entryID"></param>
        /// <returns></returns>
        public ActionResult PicShow(int albumId, int entryID)
        {
            List<Model.AlbumEntry> albumEntryList = albumEntryBLL.GetListByAlbumId(albumId);
            string firstImg = string.Empty;
            string photoHtml = string.Empty;
            if (albumEntryList.Count > 0)
            {
                if (albumEntryList[0].FAlbumEntryID != entryID && entryID != -1)//如果点击的图片不是取得列表的第一张，则设置隐藏域值，方便前台调用图片从选择图片开始播放,-1表示是从目录生成中跳转过来的，不判断从第几张开始播放
                {
                    firstImg = "img" + entryID;
                }
                System.Text.StringBuilder sbr = new System.Text.StringBuilder();
                System.Text.StringBuilder sbrUl = new System.Text.StringBuilder();

                sbr.Append("<div id=\"photos\" class=\"galleryview\">");
                sbrUl.Append("<ul class=\"filmstrip\">");

                foreach (Model.AlbumEntry acc in albumEntryList)
                {
                    sbr.Append("<div class=\"panel\">");
                    sbr.Append("<img src=\"");
                    sbr.Append(Url.Content(acc.FAlbumPhotoURL));
                    sbr.Append("\" width=\"800px\" height=\"490px\" />");
                    sbr.Append("<div class=\"panel-overlay\" style=\"text-align:center;\" >");
                    sbr.Append("<p><font  style=\"font-size:16px\">");
                    sbr.Append(acc.FAlbumNote);
                    sbr.Append("</font></p></div></div>");

                    sbrUl.Append("<li><img src=\"");
                    sbrUl.Append(Url.Content(acc.FAlbumThumbURL));
                    sbrUl.Append("\" id=\"img");
                    sbrUl.Append(acc.FAlbumEntryID);
                    sbrUl.Append("\" width=\"100px\" height=\"60px\" /> </li>");
                }

                sbrUl.Append("</ul>");
                sbr.Append(sbrUl.ToString());
                sbr.Append("</div>");

                photoHtml = sbr.ToString();
            }


            //增加背景音乐和图片时间间隔
            string bgMusicUrl = string.Empty;
            int playInterval = 6000;

            List<string> _list = albumBLL.GetBGMusicPathAndPlayInterval(albumId);
            if (_list.Count == 2)
            {
                if (_list[0].Trim() != "")
                {
                    bgMusicUrl = _list[0].Trim();
                }
                if (_list[1].Trim() != "")
                {
                    int.TryParse(_list[1].Trim(), out playInterval);
                    playInterval = playInterval * 1000;
                }
            }



            ViewData["firstImg"] = firstImg;
            ViewData["photoHtml"] = photoHtml;

            ViewData["bgMusicUrl"] = bgMusicUrl;
            ViewData["playInterval"] = playInterval;


            return View("PicShow");
        }

        /// <summary>
        /// 上传原图
        /// </summary>
        private void UploadPic(UploadedFile upload, string FAlbumClsNo, ref string errorMessage, ref string picPath)
        {
            try
            {
                //写入原图
                string entityNo = Common.GlobalConstant.ENTITYNO;
                string strYuanlaiPath = Url.Content("~/" + "Publish/" + entityNo + "/" + "album/" + FAlbumClsNo + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month.ToString().PadLeft(2, '0') + "/");
                //路径不存在就创建文件夹
                GenerateUtil.addfolder(strYuanlaiPath);
                string extension = Path.GetExtension(upload.FileName).ToLower(); //获取文件的扩展名
                string nameYuanlai = DateTime.Now.ToString("ddHHmmss") + GenerateUtil.GenerateStringId() + extension;
                picPath = strYuanlaiPath + nameYuanlai;
                FileStream fs = new FileStream(Server.MapPath(picPath), FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(upload.FileBytes);
                bw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                errorMessage = ex.ToString();
            }
        }



    }
}
